package com.electric.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.electric.model.Company;
import com.electric.model.User;
import com.electric.util.DButil;

/**
 * @author Kevin Zhang
 *
 * 2011-5-24
 */
public class CompanyService {
	public Company loadByLatLng(String lng, String lat) {
		Connection conn = DButil.createConn();
		String sql = "select * from company c where c.longitude = ? and c.latitude = ?";
		PreparedStatement ps = DButil.prepare(conn, sql);
		Company c = null;
		try {
			ps.setString(1, lng);
			ps.setString(2, lat);
			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				c = new Company();
				c.setId(rs.getInt("id"));
				c.setUp_id(rs.getInt("up_id"));
				c.setName(rs.getString("name"));
				c.setKey_man(rs.getString("key_man"));
				c.setAddress(rs.getString("address"));
				c.setPhone_no(rs.getString("phone_no"));
				c.setWebsite(rs.getString("website"));
				c.setMajor_busi(rs.getString("major_busi"));
				c.setLevel(rs.getString("level"));
				c.setLongitude(rs.getString("longitude"));
				c.setLatitude(rs.getString("latitude"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
		return c;
	}

	public void add(Company c) {
		Connection conn = DButil.createConn();
		String sql = "insert into company(id, up_id, name, key_man, address, phone_no, website, major_busi, level, longitude, latitude) values (?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = DButil.prepare(conn, sql);
		int id=0;
		try {
			if(c.getLevel() != null){
				id=getComIdByLevel(c.getLevel());
				if(id == 0){
					return;
				}
			}
			ps.setInt(1, id);
			ps.setInt(2, c.getUp_id());
			ps.setString(3, c.getName());
			ps.setString(4, c.getKey_man());
			ps.setString(5, c.getAddress());
			ps.setString(6, c.getPhone_no());
			ps.setString(7, c.getWebsite());
			ps.setString(8, c.getMajor_busi());
			ps.setString(9, c.getLevel());
			ps.setString(10, c.getLongitude());
			ps.setString(11, c.getLatitude());
			
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
	}
	
	private int getComIdByLevel(String level){
		Connection conn = DButil.createConn();
		String sql = "select MAX(id)+1 id from company where level = ?";
		PreparedStatement ps = DButil.prepare(conn, sql);
		int maxid = 0;
		try {
			ps.setString(1, level);
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()) {
				maxid = rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
		
		return maxid;
	}
	
	public HashMap<Integer, String> getComList(){
		Connection conn = DButil.createConn();
		String sql = "select id , name from company";
		PreparedStatement ps = DButil.prepare(conn, sql);
		HashMap<Integer, String> companys = new HashMap<Integer, String>();
		try {
			ResultSet rs = ps.executeQuery();
//			Company u = null;
			while(rs.next()) {
//				u = new Company();
//				u.setId(rs.getInt("id"));
//				u.setName(rs.getString("name"));
//				companys.add(u);
				companys.put(rs.getInt("id"), rs.getString("name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
//			throw(e);
		}
		DButil.close(ps);
		DButil.close(conn);
		return companys;
	}
	
	public List<Company> loadAllInfos(){
		Connection conn = DButil.createConn();
		String sql = "select * from company";
		PreparedStatement ps = DButil.prepare(conn, sql);
		List<Company> companys = new ArrayList<Company>();
		try {
			ResultSet rs = ps.executeQuery();
			Company c = null;
			while(rs.next()) {
				c = new Company();
				c.setId(rs.getInt("id"));
				c.setUp_id(rs.getInt("up_id"));
				c.setName(rs.getString("name"));
				c.setKey_man(rs.getString("key_man"));
				c.setAddress(rs.getString("address"));
				c.setPhone_no(rs.getString("phone_no"));
				c.setWebsite(rs.getString("website"));
				c.setMajor_busi(rs.getString("major_busi"));
				c.setLevel(rs.getString("level"));
				c.setLongitude(rs.getString("longitude"));
				c.setLatitude(rs.getString("latitude"));
				companys.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
//			throw(e);
		}
		DButil.close(ps);
		DButil.close(conn);
		return companys;
	}
	
	public void update(Company c) {
		Connection conn = DButil.createConn();
		String sql = "update company set name = ?, address = ? , phone_no = ? where id = ?";
		PreparedStatement ps = DButil.prepare(conn, sql);
		try {
			ps.setString(1, c.getName());
			ps.setString(2, c.getAddress());
			ps.setString(3, c.getPhone_no());
			ps.setInt(4, c.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
	}
	
	public void delete(Company c) {
		Connection conn = DButil.createConn();
		String sql = "delete from company where id = ?";
		PreparedStatement ps = DButil.prepare(conn, sql);
		try {
			ps.setInt(1, c.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.close(ps);
		DButil.close(conn);
	}
	
	public List<Company> loadAllInfosByName(String name){
		Connection conn = DButil.createConn();
		String sql = "select * from company where name like '%"+name+"%'";
		PreparedStatement ps = DButil.prepare(conn, sql);
		List<Company> companys = new ArrayList<Company>();
		try {
			ResultSet rs = ps.executeQuery();
			Company c = null;
			while(rs.next()) {
				c = new Company();
				c.setId(rs.getInt("id"));
				c.setUp_id(rs.getInt("up_id"));
				c.setName(rs.getString("name"));
				c.setKey_man(rs.getString("key_man"));
				c.setAddress(rs.getString("address"));
				c.setPhone_no(rs.getString("phone_no"));
				c.setWebsite(rs.getString("website"));
				c.setMajor_busi(rs.getString("major_busi"));
				c.setLevel(rs.getString("level"));
				c.setLongitude(rs.getString("longitude"));
				c.setLatitude(rs.getString("latitude"));
				companys.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
//			throw(e);
		}
		DButil.close(ps);
		DButil.close(conn);
		
		return companys;
	}
}
